/*
 * device_partition.c
 *
 *  Created on: 2023年11月1日
 *      Author: fjx
 */
#include "device.h"
#include "app_log.h"
#include "bsp_nvm.h"
#include "device_nvm.h"

#ifdef CONFIG_NVM_VERSION




#ifdef RFM_DEVICE
void device_rfm_formatting (void)
{
  uint16_t i=0;
  uint8_t buff[ENbyRFMUserParamDataMaxLen]={0};

  for(i=ENbyKeyRFMUserParamBase;i<=ENbyKeyRFMUserParamLast;i++){
      DS_nvmWriteWithLen(i,buff);
  }
  for(i=ENbyKeyRFMHubUserParamBase;i<=ENbyKeyRFMHubUserParamLast;i++){
      DS_nvmWriteWithLen(i,buff);
  }
  setThisParam(ENpbySystemID,buff);
  setThisParam(ENpbyPanId,buff);
}
#endif



void device_hub_formatting (void)
{
#ifdef HUB_DEVICE
  uint16_t i;
  uint8_t buff[ENbyHUBUserParamDataMaxLen]={0};


  for(i=ENbyKeyHubUserParamBase;i<=ENbyKeyHubUserParamBaseLast;i++){
      DS_nvmWriteWithLen(i,buff);
  }
  for(i=ENbyKeyRFMHubUserParamBase;i<=ENbyKeyRFMHubUserParamLast;i++){
      DS_nvmWriteWithLen(i,buff);
  }
#endif
}


void setNodeRegInfo(uint8_t id,uint8_t *codeAB,uint8_t *sn,uint8_t option,uint8_t *type)
{
#ifdef HUB_DEVICE
  uint8_t ret=0;
  uint8_t buff[NODEREGINFO_MAX_LEN]={0};

  ret = DS_nvmRead(ENbyKeyNodeRegInfoFirst+id-1,buff,NODEREGINFO_MAX_LEN);
  if(ret != 0){
      memset(buff,0,NODEREGINFO_MAX_LEN);
  }
  //copy
  memcpy(&buff[NODEREGINFO_CODEAB_OFS],codeAB,NODEREGINFO_CODEAB_LEN);
  memcpy(&buff[NODEREGINFO_SN_OFS],sn,NODEREGINFO_SN_LEN);
  memcpy(&buff[NODEREGINFO_TYPE_OFS],type,NODEREGINFO_TYPE_LEN);
  memcpy(&buff[NODEREGINFO_OPTION_OPS],&option,NODEREGINFO_OPTION_LEN);
  DS_nvmWrite(ENbyKeyNodeRegInfoFirst+id-1,buff,NODEREGINFO_MAX_LEN);
#else
  (void) id;
  (void) codeAB;
  (void) sn;
  (void) option;
  (void) type;
#endif
}

/*
 *  注册信息写入
 *  id:为子设备的ID
 *  dat:为子设备的数据,包含SN,codeAB,type,option
 */
void setNodeRegInfoPack(uint8_t id,uint8_t *dat)
{
#ifdef HUB_DEVICE
  if(!dat || id == 0 || id == 0xff){
      return;
  }
  DS_nvmWrite(ENbyKeyNodeRegInfoFirst+id-1,dat,NODEREGINFO_MAX_LEN);
#else
  (void) id;
  (void) dat;
#endif
}

void setNodeQrfH12Group(uint8_t id,uint8_t *dat)
{
#ifdef HUB_DEVICE
  if(!dat || id == 0 || id == 0xff){
      return;
  }
  DS_nvmWrite(ENbyKeyNodeQrfH12GroupFirst+id-1,dat,NodeQrfH12Group_MAX_LEN);
#else
  (void) id;
  (void) dat;
#endif
}



bool checkRFMMemSupportOTA (uint8_t id)
{
#ifdef HUB_DEVICE
  uint8_t type[3];
  uint8_t ret = 0;

  ret = getDeviceParam(id, ENpbyNodeDeviceTypeFirst, type);

  if(type[2] == HARD_TYPE_SILICON_LAB_256 || ret != 0 ){
      return false;
  }

  return true;


#else
  (void) id;
  return false;
#endif
}




























#endif



